package com.ruoyi.file.task;

import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.minio.MinioFile;
import com.ruoyi.framework.minio.MinioService;
import com.ruoyi.framework.task.AbstractTask;
import com.ruoyi.framework.task.TaskResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;


public class FileUploadTask extends AbstractTask {
    private final Logger logger = LoggerFactory.getLogger(FileUploadTask.class);
    private String file;
    private String bucket;
    private String objectName;
    private MinioService minioService;

    public FileUploadTask(String file, String bucket, String objectName) {
        this.file = file;
        this.bucket = bucket;
        this.objectName = objectName;
    }


    @Override
    public TaskResult call() {
        //        log.info(Thread.currentThread().getName() + "接收到任务：" + name + ". 当前时间：" + LocalDateTime.now().toString());
        TaskResult result = new TaskResult();
        minioService = SpringUtils.getBean(MinioService.class);
        try {
            MinioFile minioFileUpload = minioService.uploadFile(
                    new FileInputStream(new File(file)),
                    bucket, objectName);
            if (minioFileUpload == null) {
                logger.error("FileUploadTask", "upload to minio error");
            }
        } catch (Exception e) {
            logger.error("FileUploadTask", e);
        }
        return result;
    }
}
